use clap::Parser;
use image::ImageFormat;

use crate::screenshot_local::ScreenTarget;

/// 远程截图工具，命令行参数
#[derive(Parser)]
#[command(
    version,
    about = "远程截图服务",
    long_about = "远程截图服务，支持截取主屏幕或者所有屏幕，并通过 Http 协议进行响应。截取所有屏幕时拼接成一张图片返回。"
)]
pub struct Args {
    /// 服务监听地址
    #[arg(short, long, default_value_t = String::from("0.0.0.0"))]
    pub addr: String,

    /// 服务监听端口
    #[arg(short, long, default_value_t = 8520)]
    pub port: u16,

    /// 默认截取的屏幕，primary-主屏幕，all-全部屏幕
    #[arg(short, long, default_value = "primary", value_parser = screen_passer)]
    pub screen: ScreenTarget,

    /// 默认图片格式，暂且仅支持 jpg、png 两种格式
    #[arg(short, long, default_value = "jpg", value_parser = format_passer)]
    pub format: ImageFormat,
}

/// 目标屏幕参数格式器
fn screen_passer(screen: &str) -> Result<ScreenTarget, String> {
    match screen.to_uppercase().as_str() {
        "PRIMARY" | "" => Ok(ScreenTarget::Primary),
        "ALL" => Ok(ScreenTarget::All),
        _ => Err("参数仅支持 primary 或 all".to_owned()),
    }
}

/// 图片格式参数格式器
fn format_passer(format: &str) -> Result<ImageFormat, String> {
    match format.to_uppercase().as_str() {
        "JPG" | "JPEG" | "" => Ok(ImageFormat::Jpeg),
        "PNG" => Ok(ImageFormat::Png),
        _ => Err("参数仅支持 jpg、jpeg 或 png".to_owned()),
    }
}
